package com.linwen.excise.huaweiOJ;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;

/**
 * @ClassName 单词出现次数排序
 * @Description 次数降序排,相同次数按字母升序排
 * @Author lin
 * @Date 2022/4/15 上午12:47
 * @Version V1.0
 */
public class 单词出现次数排序 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        String[] s = str.split(" ");
        function(s);
    }
    private static void function(String[] strings) {
        // map存不同的字符串+统计次数
        HashMap<String,Integer> map = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            if(map.containsKey(strings[i])){
                Integer i1 = map.get(strings[i]);
                i1++;
                map.put(strings[i],i1);
            }else{
                map.put(strings[i],1);
            }
        }

        for(String key:map.keySet()){
            System.out.println(key+"  "+map.get(key));
        }
        // 排序/
        LinkedList<Map.Entry<String,Integer>> list = new LinkedList(map.entrySet());
        list.sort((o1, o2) -> {
            if(o1.getValue().compareTo(o2.getValue())<0){
                return 1;
            }
            else if(o1.getValue().compareTo(o2.getValue())>0){
                return -1;
            }
            else {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        System.out.println("===============");
        for(Map.Entry<String, Integer> temp:list){

            System.out.println(temp.getKey());
        }
// hello every my name is hello and my name is hello
    }
}
